﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cjhushi
{
    public class ReliefCounter
    {
        class Refugee :IComparable
        {
            private int id;
            private int volumn;

            public Refugee(int id,int volumn)
            {
                this.id = id;
                this.volumn = volumn;
            }

            public int CompareTo(object? obj)
            {
                if(obj!=null)
                {
                    if (volumn < ((Refugee)obj).volumn)
                        return -1;
                    if (volumn > ((Refugee)obj).volumn)
                        return 1;
                    if (id < ((Refugee)obj).id)
                        return -1;
                    return 1;
                }
                return 0;
            }

            public void updateVolumn()
            {
                volumn += 3;
            }
            public int getId()
            {
                return this.id;
            }
        }
        private int count;
        private int numberRefugee;

        private List<Refugee> refugees = new List<Refugee>();
        private HashSet<int> idSet = new HashSet<int>();
        public int getResult(string input)
        {
            handleInput(input);
            sort();
            count=calResult();
            return count;
        }
        private void handleInput(string input)
        {
           count = 0;
            string[] strInput = input.Split(" ");
            numberRefugee = int.Parse(strInput[0]);
            for(int i=1;i<strInput.Length;i++)
            {
                Refugee refugee = new Refugee(i, int.Parse(strInput[i]));
                refugees.Add(refugee);
            }
        }
        private void sort()
        {
            refugees.Sort();
        }
        private int calResult()
        {
            for(int i=0;i<numberRefugee;i++)
            {
                Refugee refg= refugees[0];
                int id = refg.getId();
                idSet.Add(id);
                refugees.Remove(refg);
                refg.updateVolumn();
                refugees.Add(refg);
                refugees.Sort();
                
            }
            return numberRefugee-idSet.Count;
        }
    }
}
